package com.java.module.system.controller.api;

import java.util.ArrayList;
import java.util.List;

import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.entity.DataName;
import com.java.common.lang.domain.entity.SysDictData;
import com.java.common.utils.StringUtils;
import com.java.module.system.service.ISysDictDataService;
import com.java.module.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.java.common.lang.annotation.ValidateToken;
import com.java.module.system.domain.Region;
import com.java.module.system.domain.dto.RegionDTO;
import com.java.module.system.domain.dto.UploadDTO;
import com.java.module.system.domain.vo.UploadVO;
import com.java.module.system.service.ICommonService;
import com.java.module.system.service.IRegionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 通用请求处理
 * 
 * @author yilabao
 * @date 2021年3月22日
 */
@ApiSort(0)
@Api(tags = "通用接口")
@RestController("ApiCommonController")
@RequestMapping("/common")
public class CommonController {

    @Autowired
    ICommonService commonService;

    @Autowired
    IRegionService regionService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("上传接口")
    @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class, required = true)
    @PostMapping("/upload")
    public UploadVO uploadFile(@RequestParam("file") MultipartFile file) {
        return commonService.uploadFile(file);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("base64上传接口")
    @PostMapping("/uploadBase64")
    public UploadVO uploadBase64(@Validated @RequestBody UploadDTO uploadDTO) {
        return commonService.uploadBase64(uploadDTO.getFile());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("地区接口")
    @GetMapping("/region/list")
    @ValidateToken(isValidate = false)
    public List<Region> regionList(@Validated RegionDTO regionDTO) {
        return regionService.selectRegionList(regionDTO);
    }


    /**
     * 查询字典数据详细
     */
    @ValidateToken(isValidate = false)
    @GetMapping(value = "/data/{dictCode}")
    public AjaxResult getInfo(@PathVariable String dictCode) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictCode);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }




}